package com.hkfn.rfb.common.fragment;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hkfn.rfb.R;
import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.utils.StringUtils;
import com.hkfn.rfb.common.utils.WebViewSettingUtils;
import com.hkfn.rfb.common.view.GetTopWebView;
import com.hkfn.rfb.common.view.InitView;
import com.hkfn.rfb.common.view.SuperSwipeRefreshLayout;

public class WebFragment extends BaseFragment implements SuperSwipeRefreshLayout.OnSwipeRefreshListener {
    private BaseActivity activity;
    private View itemContentView;

    private View include_head_title;
    private TextView tv_title;
    private ImageView iv_back;

    private SuperSwipeRefreshLayout swipeLayout;
    private WebView webview;
    private WebViewClient webViewClient;
    private WebChromeClient webChromeClient;

    private Bundle bundle;
    private String url;

    private boolean isFirstLoad = true;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            activity = getMyActivity();
            bundle = getArguments();
            if (null != bundle){
                url = bundle.getString("url");
            }
            itemContentView = inflater.inflate(R.layout.f_web, container, false);
            initView();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {
        initHeadView();
        swipeLayout = itemContentView.findViewById(R.id.swipe_container);
        swipeLayout.setOnSwipeRefreshListener(this);

        webview = (GetTopWebView) itemContentView.findViewById(R.id.gtwv_web);
        if (url.contains("needRefresh=0")){
            swipeLayout.setEnabled(false);
            ((GetTopWebView)webview).setNeedRefresh(false);
        }else {
            InitView.instance().initSuperSwipeLayout(swipeLayout);
        }
        webview.setVerticalScrollbarOverlay(false);
        ((GetTopWebView)webview).setSwipeRefreshLayout(swipeLayout);
        webview.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
        initWebView();

        if (!StringUtils.isEmpty(url)){
            webview.loadUrl(url);
        }
    }

    private void initHeadView() {
        include_head_title = itemContentView.findViewById(R.id.include_head_title);
        include_head_title.setVisibility(View.GONE);
        tv_title = itemContentView.findViewById(R.id.tv_title);
        if (bundle != null && bundle.containsKey("title")) {
            tv_title.setText(bundle.getString("title"));
        }
        iv_back = itemContentView.findViewById(R.id.iv_back);
        iv_back.setVisibility(View.GONE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != webview && webview.canGoBack()){
                    webview.goBack();
                    if (webview.canGoBack()){
                        iv_back.setVisibility(View.VISIBLE);
                    }else {
                        iv_back.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    /**
     * 初始化webview
     */
    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        webview = WebViewSettingUtils.getSettingInstance(webview, activity);
        webChromeClient = new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                swipeLayout.setRefreshing(false);
                tv_title.setText(title);
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     JsResult result) {
                // TODO Auto-generated method stub
                return super.onJsAlert(view, url, message, result);
            }
        };
        webview.setWebChromeClient(webChromeClient);
        webViewClient = new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.e("startUrl", url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                Log.e("shouldOverrideUrl", url);
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Log.e("finishUrl", url);
            }
        };
        webview.setWebViewClient(webViewClient);

        //setJsBridge();
    }


    @Override
    public void onSwipeRefresh() {
        swipeLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(false);
                if (!StringUtils.isEmpty(url)){
                    webview.loadUrl(url);
                }
            }
        }, 2000);
    }

    @Override
    public void onPushDistanceMoreAction(int distance) {}

    @Override
    public void onPullEnableMoreAction(boolean enable) {}

    @Override
    public void onResume() {
        super.onResume();
        Log.e("xuhan", "onResume");
        if (!isFirstLoad){
            activity.finish();
        }
        isFirstLoad = false;
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e("xuhan", "onPause");
    }
}
